// pages/doctorPages/my/addModal/addModal.js
var app = getApp();
const ajax = require("../../../.././utils/ajax.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    test:{},
    rules: {
      id: "",
      weeks: [],
      weekStr: "",
      morning: {
        number: 1,
        special: 1,
        special_number: 1,
        members: {
          1: "9:00"
        },
        special_rules: {
          before: 2,
          time_type: 2,
          money: "10.00"
        },
        before_time: 1,
        time_type: 2
      },
      miss_times: {
        "1": {
          time: "3",
          time_type: "1"
        },
        "2": {
          time: "24",
          time_type: "1"
        },
        "3": {
          time: "3",
          time_type: "2"
        },
        "more": {
          money: "10.00"
        }
      },
      number: 0,
      remark: "",
      cancle: 2,
      afternoon: {
        number: 1,
        special: 0,
        special_number: 1,
        members: {
          1: "14:00"
        },
        special_rules: {
          before: 2,
          time_type: 2,
          money: "10.00"
        },
        before_time: 1,
        time_type: 2
      },
      afternoon_begin: "13:30",
      afternoon_end: "17:30",
      morning_begin: "7:00",
      morning_end: "11:30"
    }  
  },
  changeCacel:function(e){
    //取消规则
    let data=this.data.rules
    data.cancle=e.detail.value
    this.setData({
      rules:data
    })
  },
  changeMIssTime(e) {
    let i = e.currentTarget.dataset.key;
    let rules = this.data.rules;
    rules.miss_times[i].time_type = e.currentTarget.dataset.time;
    this.setData({
      rules: rules
    })
  },
  changeMissValue(e) {
    //失约规则
    let i = e.currentTarget.dataset.key;
    let rules = this.data.rules;
    if (i <= 3) {
      rules.miss_times[i].time = e.detail.value;
    } else {
      rules.miss_times[i].money = parseFloat(e.detail.value).toFixed(2);
    }

    this.setData({
      rules: rules
    })
  },
  bindTimeChange: function (e) {
    console.log(e)
    let t = e.currentTarget.dataset.value;
    let value = e.detail.value;
    let r = this.data.rules;
    if (t == 'mb') {
      r.morning_begin = value;
    } else if (t == 'me') {
      r.morning_end = value;
    } else if (t == 'ab') {
      r.afternoon_begin = value;
    } else if (t == 'ae') {
      r.afternoon_end = value;
    }
    this.setData({
      rules: r
    })    
  },
  //日期设置
  dateSet: function () {
    wx.navigateTo({
      url: '../dateList/dateList?rules=' + JSON.stringify(this.data.rules),
    })
  },
  orderPerson: function () {
    console.log(JSON.stringify(this.data.rules))
    wx.navigateTo({
      url: '../orderPerson/orderPerson?rules=' + JSON.stringify(this.data.rules),
    });
  },
  setRemark(e) {
    let rules = this.data.rules;
    rules.remark = e.detail.value
    this.setData({
      rules: rules
    });
  },
  setWeeks(weeks) {
    let wstr = "";
    let k = 0;
    for (var i in weeks) {
      if (k >= 2) {
        wstr += '...';
        break;
      }
      k++;
      if (wstr) {
        wstr += "," + app.weeks[weeks[i]]
      } else {
        wstr += app.weeks[weeks[i]]
      }
    }
    return wstr;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = options.id
    
    if (id) {
      wx.showLoading({
        title: '拼命加载中..',
      })
      ajax.request({
        url: app.url.doctorRulesDetail,
        data: { id: id }
      }).then(res => {
        if (res.code != 200) {
          return wx.showToast({
            title: res.msg,
            icon: "none"
          })
        }
        if (res.data.morning) {
          res.data.number = parseInt(res.data.afternoon.number) + parseInt(res.data.morning.number) 
          this.setData({
            rules: res.data
          })
        }
        this.setWeekStr()
      })
    }
  },
  setNumber(){
    let an = parseInt(this.data.rules.morning.number)
  },
  setWeekStr() {
    wx.hideLoading()
    let rule = this.data.rules;
    console.log(rule)
    let weekStr = this.setWeeks(rule.weeks);
    rule.weekStr = weekStr;
    this.setData({
      rules: rule
    })
  },
  setDoctorRules(){
    let rules = this.data.rules;
    console.log(rules)
    ajax.request({
      url: app.url.doctorSetRules,
      data: rules
    }).then(res => {
      if (res.code != 200) {
        return wx.showToast({
          title: res.msg,
          icon: "none"
        })
      }
      wx.navigateBack({
       delta:1 
      })
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let options=this.data.test
    var rules = options;
    
    if (JSON.stringify(options)!=="{}") {
      console.log(options)
      wx.showLoading({
        title: '拼命加载中..',
      })
      let r = rules;
      r.number = r.afternoon.number + r.morning.number
      this.setData({
        rules: r
      })
      this.setWeekStr()                                        
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})